﻿using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Toybox.SCM.Models.Accounts
{
	using NicoSharp.Utilities;

	/// <summary>
	/// PriorityAccounts
	/// </summary>
	public class PriorityAccountCollection : ObservableCollection<PriorityAccount>
	{

		#region Public Methods

		/// <summary>
		/// アカウント取得
		/// </summary>
		/// <param name="uId"></param>
		/// <returns></returns>
		public PriorityAccount this[string uId]
		{
			get
			{
				foreach (var pa in this)
				{
					if (pa.Account.UId == uId)
						return pa;
				}
				return null;
			}
		}

		/// <summary>
		/// 優先度リスト取得
		/// </summary>
		/// <param name="priority"></param>
		/// <returns></returns>
		public IEnumerable<PriorityAccount> GetAccounts(uint priority)
		{
			foreach (var pa in this)
			{
				if (pa.Priority == priority)
				{
					yield return pa;
				}
			}
		}

		/// <summary>
		/// 優先度一覧の取得
		/// </summary>
		/// <returns></returns>
		public uint[] GetPriorities()
		{
			var result = new List<uint>();
			foreach (var pa in this)
			{
				if (!result.Contains(pa.Priority))
					result.Add(pa.Priority);
			}

			result.Sort();

			return result.ToArray();
		}

		/// <summary>
		/// アカウントの追加
		/// </summary>
		public void AddAccounts(AccountCollection accounts)
		{
			foreach (var account in accounts)
				this.Add(new PriorityAccount(account));
		}

		#endregion Public Methods

		#region OverrideMethods

		/// <summary>
		/// 文字列化
		/// </summary>
		/// <returns></returns>
		public override string ToString()
		{
			string result = string.Empty;
			var isUseList = new List<string>();
			var priorityList = new List<string>();
			var uIdList = new List<string>();

			foreach (var account in this)
			{
				isUseList.Add(account.IsUse.ToString());
				priorityList.Add(account.Priority.ToString());
				uIdList.Add(account.Account.UId);
			}

			result += CsvUtility.Compose(uIdList.ToArray()) + "\n";
			result += CsvUtility.Compose(isUseList.ToArray()) + "\n";
			result += CsvUtility.Compose(priorityList.ToArray()) + "\n";

			return result;
		}

		#endregion OverrideMethods

	}
}
